    <div class="head"><h5 class="iList">Root Category for <?php echo $directory->getName() ?></h5></div>
     <form action="<?php echo url_for('categories/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" id="valid" class="mainForm" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
     	<div class="rowElem noborder">
     	<label for="name">Name:</label>
     		<div class="formRight"><input type="text" name="category[name]" value="<?php echo $form->getObject()->getName()?>"  class="validate[required]"  id="category_name" style="width: 500px" /></div>
     		<div class="fix"></div>
     	</div>
                   <input type="hidden" name="category[master_directory_id]" id="category_master_directory_id" style="width:240px;" value="<?php echo $directory->getId()?>"  />
     	
     	<div class="fix"></div>
        <input type="button" value="Save" class="greyishBtn submitForm" id="save" />
     	<?php if (!$form->getObject()->isNew()){
     		?>
     		<input type="button" value="Create New" class="greyishBtn submitForm" id="create_new" />
     	<?php 
     	} ?>          
        <div class="fix"></div>
     </form>
     
     
<script type="text/javascript">

$("#save").click(function (event){
    submitForm();
})

$("#create_new").click(function (event){

    $.ajax({type: 'POST', url:' <?php echo url_for('categories/index')?>', script:true, cache:false,
            success: function(data) {
            	$('#category_container_div').html(data);
                $('#category_name').attr('value',"");
             } });
             
})

$("form#valid").submit(function (event){
    event.preventDefault();
    submitForm();
})

function submitForm(){
    var str=$('form#valid').serialize();

    $.ajax({type: 'POST', url:' <?php echo url_for('categories/'.($form->getObject()->isNew() ? 'create' :'update').
    (!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : ''))?>', script:true, cache:false, data:  str, 
    beforeSend: function(){
        if (!validate()){
			return false;
        }
         },
            success: function(data) {
                 $('#category_container_div').html(data);
                 $('#category_name').attr('value',"");
             } });
}

		function loadCategoryData (){

			var php_url = '<?php echo url_for('categories/loadCategoryData?directory_id=')?>';
			var directory_id = document.getElementById('category_master_directory_id').value;
			php_url = php_url.concat(directory_id);	
			
			$.ajax({type: 'POST', url:php_url, script:true, cache:false, 
				success: function(data) { 
					 $('#table').html(data);
					 $('#templates_name').attr('value',"");
					 
					} });
			
		}

		function validate(){
			var valid = true;
			var error_msg = "";
			if ($("#category_name").attr("value") == ""){
				valid = false;
				error_msg = "Category name required";
			};	
				
			if (!valid){
				$('#msg_text').html(error_msg);
				$('#error_msg').show('fast');
			}
			return valid;			
		}
		
		$('form').jqTransform({imgPath:'../images/forms'});
</script>     
